<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Project_participater extends CI_Controller{
	public function __construct() {
		parent::__construct ();
		$this->load->model("project_participater_model");
		$this->load->model("project_model");
		$this->load->model("user_model");
		$this->load->model("project_content_comment_model");
		$this->load->model("project_content_model");
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	public function index($pid=1){
		$whereArr = array("pid"=>$pid,"status"=>1);
		$project = $this->project_model->row($whereArr);
		$project['username'] = $this->change_uid_to_username($project['uid']);
		$project['logo'] = ltrim($project['logo'],".");
		$whereArr = array("pid"=>$pid,'check'=>2,'status'=>1);
		$time_different = $this->count_day($project['starttime'], $project['addtime']);
		if($time_different){
			$project['time_different'] = $time_different;
		}
		$project_participater_count = $this->project_participater_model->count($whereArr);
		$project_participater_list = $this->project_participater_model->resultAll($whereArr);
		foreach($project_participater_list as $key => $value){
			$project_participater_list[$key]['username'] = $this->change_uid_to_username($value['uid']);
		}
		$data['project'] = $project;
		$data['project_participater_count'] = $project_participater_count;
		$data['project_participater_list'] = $project_participater_list;
		$whereArr = array("status"=>1,"check"=>3,"pid"=>$pid);
		$project_content_list = $this->project_content_model->resultAll($whereArr,"sort@DESC");
		foreach($project_content_list as $key =>$value){
			$project_content_list[$key]['comment'] = $this->find_content_comment($value['pcid']);
		}
		$data['project_content_list'] = $project_content_list;
		$data['category_list'] = $this->category_list;
		$this->load->view("project_join_detail",$data);
	}
	/*
	 *参与项目 
	 */
	public function join_project($pid=1){
		if(!isset($_SESSION['username'])&&empty($this->session->userdata('username'))){
			$message['flag'] = -1;
			$message['content'] = "登录之后才能参加,请先登录!!";
			$message['url'] = site_url('Login');
			$this->show_message($message);
		}
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid"=>$uid,'pid'=>$pid);
		$project_participater_message = $this->project_participater_model->row($whereArr);
		if($project_participater_message){
			if(($project_participater_message['check']!=0)&&($project_participater_message['status']==1)){
				$message['flag'] =-1;
				$message['content'] = "别闹你已经参加该项目了,或还在审核当中!!";
				$this->show_message($message);
			}
			if($project_participater_message['check']==0){
				$message['flag'] = -1;
				$message['content'] = "不好意思,你已经被拉黑,不能再参加该项目了!";
				$this->show_message($message);
			}
		}
		$project_message = $this->project_message($pid);
		$whereArr = array("pid"=>$pid,'check'=>2,'status'=>1);
		$project_participater_count = $this->project_participater_model->count($whereArr);
		$project_max_num = $project_message['personNum'];
		if($project_participater_count>=$project_max_num){
			$message['flag'] = -1;
			$message['content'] = "参加人数已满,不能再参加了!!";
			$this->show_message($message);
		}
		if($project_participater_message){
			$dataArr = array("check"=>1,"status"=>1);
			$whereArr = array("pid"=>$pid,"uid"=>$uid);
			$result = $this->project_participater_model->update($dataArr,$whereArr);
			if($result){
				$message['flag'] = 1;
				$message['content'] = "重新加入该项目成功!";
				$this->show_message($message);
			}else{
				$message['flag'] = -1;
				$message['content']="重新加入该项目失败!";
				$this->show_message($message);
			}
		}else{
			$project_username = $this->find_project_creater($pid);
			$data['uid'] = $this->change_username_to_uid($this->session->userdata('username'));
			$data['pid'] = $pid;
			$data['createtime'] = time();
			if($project_username==$this->session->userdata('username')){
				$data['isparticipater'] = 1;
			}else{
				$data['isparticipater'] = 0;
			}
			$result = $this->project_participater_model->insert($data);
			if($result){
				$message['flag'] = 1;
				$message['content'] = "参加成功,请等待审核!!";
				$this->show_message($message);
			}else{
				$message['flag'] = -1;
				$message['content'] = "参加失败!!";
				$this->show_message($message);
			}
		}
	}
	/*
	 *计算项目剩余天数 
	 */
	public function count_day($starttime,$addtime){
		$nowtime = time();
		$time_difference_day = round(($nowtime - $starttime)/3600/24);
		if($time_difference_day<$addtime){
			return ($addtime-$time_difference_day);
		}else{
			return false;
		}
	}
	/*
	 *消息显示 
	 */
	public function show_message($message){
		echo json_encode($message);
		exit;
	}
	/*
	 *查找项目信息
	 */
	public function project_message($pid){
		$whereArr = array('pid'=>$pid);
		$project_message = $this->project_model->row($whereArr);
		if($project_message){
			return $project_message;
		}
	}
	/*
	 *查找项目的发起者 
	 */
	public function find_project_creater($pid){
		$whereArr = array('pid'=>$pid);
		$project_user = $this->project_model->row($whereArr);
		if($project_user){
			$username = $this->change_uid_to_username($project_user['uid']);
			return $username;
		}
	}
	/*
	 *根据用户id转换为用户名 
	 */
	public function change_uid_to_username($uid){
		$whereArr = array("uid"=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user['username'];
		}
	}
	/*
	 *根据用户名转换为用户id 
	 */
	public function change_username_to_uid($username){
		$whereArr = array("username"=>$username);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user['uid'];
		}
	}
	/*
	 *查找每个项目的内容的评论
	 */
	public function find_content_comment($pcid){
		$comment_show_message = array();
		$whereArr = array("pcid"=>$pcid,"status"=>1);
		$project_content_comment_list = $this->project_content_comment_model->resultCount($whereArr,2,"createtime@DESC");
		foreach($project_content_comment_list as $key =>$value){
			$comment_show_message[$key]['content'] = $value['content'];
			$comment_show_message[$key]['user_logo'] = $this->find_user_logo($value['uid']);
			$comment_show_message[$key]['user_id'] = $this->find_user_id($value['uid']);
		}
		return $comment_show_message;
	}
	/*
	 *查找用户头像信息
	 */
	public function find_user_logo($uid){
		$whereArr = array('uid'=>$uid);
		$user_message = $this->user_model->row($whereArr);
		return $user_message['logo'];
	}
	/*
	 *查找用户id
	 */
	public function find_user_id($uid){
		$whereArr = array('uid'=>$uid);
		$user_message = $this->user_model->row($whereArr);
		return $user_message['uid'];
	}
}